<?php

/**
 * This is the model class for table "cabs_library".
 *
 * The followings are the available columns in table 'cabs_library':
 * @property integer $id
 * @property string $name
 * @property string $slug
 * @property integer $gameId
 * @property integer $cabinetId
 * @property integer $shelf
 * @property integer $parentId
 * @property string $donor
 * @property string $create_time
 * @property integer $create_user_id
 * @property string $update_time
 * @property integer $update_user_id
 *
 * The followings are the available model relations:
 * @property LibraryStatus[] $statuses
 * @property Cabinet $cabinet
 * @property Game $game
 */
class Library extends CabsActiveRecord
{
		/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Library the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cabs_library';
	}

	/**
	 * @variables for seeking boxed-in items
	 */
	 protected $_additions;
	 public $game_grid;
	 public $parent_grid;
	 public $cabinet_grid;

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, gameId, cabinetId', 'required'),
			array('gameId, cabinetId, shelf, parentId', 'numerical', 'integerOnly'=>true),
			array('name, slug, donor', 'length', 'max'=>255),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, name, slug, gameId, game_grid, cabinetId, cabinet_grid, shelf, parentId, parent_grid, donor, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'baseGame'	=> array(self::BELONGS_TO, 'Library', 'parentId'),
			'cabinet' => array(self::BELONGS_TO, 'Cabinet', 'cabinetId'),
			'game' => array(self::BELONGS_TO, 'Game', 'gameId'),
			'includedGames' => array(self::HAS_MANY, 'Library', 'parentId'),
			'statuses' => array(self::HAS_MANY, 'LibraryStatus', 'libraryId'),
		);
	}
	public function scopes()
	{
		return array(
			'isActive'=>array(
				'condition'=>"t.cabinetId>4",
			),
		);
	}
	
	public function baseLibrary()
	{
		$this->getDbCriteria()->mergeWith(array(
				'condition'=>"t.parentId < 1 OR t.parentId IS NULL",
		));
		return $this;
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Library Title',
			'slug' => 'Slug',
			'gameId' => 'Game',
			'game_grid' => 'Game',
			'cabinet' => 'Cabinet',
			'cabinet_grid' => 'Cabinet',
			'shelf' => 'Shelf',
			'parentId' => 'Boxed With Base Game',
			'parent_grid' => 'Boxed With Base Game',
			'donor' => 'Donor',
			'create_time' => 'Created On',
			'create_user_id' => 'Created By',
			'update_time' => 'Last Updated On',
			'update_user_id' => 'Last Updated By',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todoWarning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with = array('game','cabinet','baseGame');

		$criteria->compare('t.id',$this->id);
		$criteria->compare('t.name',$this->name,true);
		$criteria->compare('t.slug',$this->slug,true);
		$criteria->compare('game.name',$this->game_grid, true);
		$criteria->compare('gameId',$this->gameId);
		$criteria->compare('cabinetId',$this->cabinet);
		$criteria->compare('cabinet.name',$this->cabinet_grid, true);
		$criteria->compare('shelf',$this->shelf);
		$criteria->compare('parentId',$this->parentId);
		$criteria->compare('library.name',$this->parent_grid, true);
		$criteria->compare('donor',$this->donor,true);
		$criteria->compare('create_time',$this->create_time,true);
		$criteria->compare('create_user_id',$this->create_user_id);
		$criteria->compare('update_time',$this->update_time,true);
		$criteria->compare('update_user_id',$this->update_user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				'attributes'=>array(
					'game_grid'=>array(
						'asc'=>'game.name',
						'desc'=>'game.name DESC',
					),
					'parent_grid'=>array(
						'asc'=>'baseGame.name',
						'desc'=>'baseGame.name DESC',
					),
					'cabinet_grid'=>array(
						'asc'=>'cabinet.name',
						'desc'=>'cabinet.name DESC',
					),
					'*',
				),
			),
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Library the static model class
	 */
	 /*
	 * Behaviors for this model
	 */
	public function behaviors(){
		return array(
			'sluggable' => array(
				'class'=>'ext.behaviors.SluggableBehavior.SluggableBehavior',
				'columns' => array('name'),
				'unique' => true,
				'update' => false,
			),
		);
	}
	
	// to get the additions...

	public function getAdditions(){
        if(null === $this->_additions){
                $this->_additions = Library::model()->findAll('parentId='.$this->id);
        }
        return $this->_additions;
	}

	// to get base game
	public function getBaseGame(){
        
        if($this->parentId)  // isnt root?
        {

                $model = Library::model()->findByPk($this->parentId);
                return $model;
        }
        return null;
	}
	
	// to get cabinet options
	public function getCabinetOptions()
	{
		$cabinetModel = Cabinet::model()->findAll(
			array('order'=>'name'));
		$cabinetArray = CHtml::listData($cabinetModel, 'id','name');
		return $cabinetArray;
	}
	public function getGameDisplay()
	{
		return $this->name;
	}

}
